/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class LinkedList {
    public Node head;

//    public void creatList() {
//        Node node1 = new Node(12);
//        Node node2 = new Node(23);
//        Node node3 = new Node(34);
//        Node node4 = new Node(45);
//        Node node5 = new Node(56);
//
//        node1.next = node2;
//        node2.next = node3;
//        node3.next = node4;
//        node4.next = node5;
//        this.head = node1;
//    }

    //打印单链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //按head打印单链表
    public void display2(Node newHead) {
        Node cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = this.head;
        this.head = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contain(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (key == cur.val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        } else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
            node.next = null;
        }
    }

    public Node findIndex(int index) {
        Node cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个节点为0下标
    public void addIndex(int index, int data) {
        Node node = new Node(data);
//        Node cur = this.head;
//        Node pos = cur;
//        int count = 0;
//        while(pos != null){
//            pos = pos.next;
//            count++;
//        }
        if (index < 0 || index >= size()) {
            System.out.println("index的输入有误！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            System.out.println("链表为空！");
            return;
        }
        Node cur = this.head;
        Node pos = cur;
        if (cur.val == key) {
            this.head = cur.next;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
            if (cur.val != key) {
                pos = cur;
            } else {
                pos.next = cur.next;
                return;
            }
        }
        System.out.println("没有这个节点！");
    }

    //删除所有值为key的节点
    public Node removeAllKey(int key) {
        if (this.head == null) {
            System.out.println("链表为空！");
            return null;
        }
        Node pos = this.head;
        Node cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                pos.next = cur.next;
                cur = cur.next;
            } else {
                pos = cur;
                cur = cur.next;
            }
        }

        if (head.val == key) {
            this.head = this.head.next;
        }
        return this.head;
    }

    //清空链表
    public void clean() {
        Node curNext = this.head;
        Node cur = curNext;
        this.head = null;
        while (curNext != null) {
            cur.next = null;
            curNext = curNext.next;
            cur = curNext;
        }
    }

    public Node reverseList() {
        if(head == null){
            return null;
        }

        Node prev = null;
        Node cur = head;
        Node curNext = head.next;

        while(cur != null){
            curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }

    //返回链表的中间节点
    public Node middleNode() {
        if(head == null){
            return null;
        }

        Node fast = this.head;
        Node slow = this.head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //链表中倒数第k个节点
    public Node FindKthToTail(int k) {
        if(head == null || k<0){
            return null;
        }

        Node fast = head;
        Node slow = head;
        while( k - 1 != 0 ){
            fast = fast.next;
            if(fast == null){
                return null;
            }
            k--;
        }

        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //链表分割
    public Node partition(int x) {
        // write code here
        Node newHeadA = null;
        Node newHeadB = null;
        Node cur = head;
        Node curA = newHeadA;
        Node curB = newHeadB;

        while(cur != null){
            if(cur.val < x){
                if(newHeadA == null){
                    newHeadA = cur;
                    curA = cur;
                }else{
                    curA.next = cur;
                    curA = curA.next;
                }
            }else{
                if(newHeadB == null){
                    newHeadB = cur;
                    curB = cur;
                }else{
                    curB.next = cur;
                    curB = curB.next;
                }
            }
            cur = cur.next;
        }
        if(newHeadA == null){
            return newHeadB;
        }
        curA.next = newHeadB;
        if(newHeadB != null){
            curB.next = null;
        }
        return newHeadA;
    }

    //删除链表中重复的值
    public Node deleteDuplication() {
        if(head == null){
            return null;
        }
        Node newHead = new Node(-1);
        Node tmp = newHead;
        Node cur = head;

        while(cur != null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                cur = cur.next;
            }else{
                tmp.next = cur;
                tmp = cur;
                cur = cur.next;
            }
        }
        tmp.next = null;
        return newHead.next;
    }

    //链表的回文结构
    public boolean chkPalindrome() {
        if(head == null){
            return true;
        }
        Node fast = head;
        Node slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        Node cur = slow.next;

        while(cur != null){
            Node curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        while(head != slow){
            if(slow.val == head.val){
                slow = slow.next;
                head = head.next;
                if(head.next == slow){
                    return true;
                }
            }else{
                return false;
            }
        }
        return true;
    }

    //判断链表是否有环
    public boolean hasCycle() {
        if(head == null){
            return false;
        }

        Node slow = head;
        Node fast = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return false;
        }else{
            return true;
        }
    }

    //链表可以入环的第一个节点
    public Node detectCycle() {
        if(head == null){
            return null;
        }

        Node slow = head;
        Node fast = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        fast = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }

    //
    public Node swapNodes(int k) {
        if(head == null){
            return null;
        }

        //1、求链表长度
        int size = 0;
        Node pos = head;
        while(pos != null){
            size++;
            pos = pos.next;
        }

        //2、反转链表
        Node slow = head;
        Node fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        Node cur = slow.next;
        while(cur != null){
            Node curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        //3、根据k走
        Node sign = head;
        Node prevSlow = slow;
        Node prevSign = head;
        for(int i = k-1; i != 0; i--){
            prevSlow = slow;
            slow = slow.next;
            prevSign = sign;
            sign = sign.next;
        }

        //4、交换
        prevSlow.next = sign;
        prevSign.next = slow;
        sign.next = slow.next;
        slow.next = sign.next;
        return head;
    }
}



class Node{
    public int val;
    public Node next;

    public Node(int val){
        this.val = val;
    }
    public Node(){}
}